#include <ncurses.h>
#include <stdlib.h>
#include <stdint.h>
#include "util.h"

uint8_t check_size(int maxy, int maxx)
{
	if (maxy < 24 || maxx < 80) {
		endwin();
		fputs("ERROR: terminal must be at least 24x80\n", stderr);
		return 0;
	}
	return 1;
}

uint8_t init_curses(int* maxy, int* maxx)
{
	initscr();
	getmaxyx(stdscr, *maxy, *maxx);
	if(! check_size(*maxy, *maxx))
		return 0;
	cbreak(); noecho(); nonl(); keypad(stdscr, TRUE);
	curs_set(0);
	wnoutrefresh(stdscr);
	return 1;
}

uint8_t handle_input(WINDOW* game_window, int* maxy, int* maxx)
{
	int input = getch();
	switch(input) {
		case 'q':
		case 'Q':
			return 0;
		case KEY_RESIZE:
			getmaxyx(stdscr, *maxy, *maxx);
			if(! check_size(*maxy, *maxx))
				return 0;
			wresize(game_window, 24, 80);
			box(game_window, 0, 0);
			wnoutrefresh(game_window);
			break;
	}
	return 1;
}

